package org.wk.user.service;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.wk.account.domain.ling.SubAccountLingSeq;
import org.wk.core.common.view.ResponseVo;
import org.wk.core.db.tool.WebUtils;
import org.wk.user.dao.UserDao;
import org.wk.user.dao.UserPromoteRelationDao;
import org.wk.user.domain.User;
import org.wk.user.domain.UserPromoteRelation;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserPromoteRelationServiceImpl implements UserPromoteRelationService{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //推广级别设置
    private final static int PROMOTE_LEVEL = 9999;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserPromoteRelationDao userPromoteRelationDao;

    @Override
    public boolean addRelationship(String promoter, String bePromoter) {
        UserPromoteRelation userPromoteRelation = userPromoteRelationDao.findByParentIdAndUserId(promoter, bePromoter);
        //如果已经存在关系则不能新增
        if (userPromoteRelation != null) {
            return false;
        }
        List<UserPromoteRelation> needSaveData = new ArrayList<>();
        //添加直属关系
        userPromoteRelation = new UserPromoteRelation();
        userPromoteRelation.setParentId(promoter);
        userPromoteRelation.setRelationship(1);
        userPromoteRelation.setUserId(bePromoter);
        needSaveData.add(userPromoteRelation);

        //查询推广者的所有关系
        List<UserPromoteRelation> userPromoteRelationList = userPromoteRelationDao.findByUserId(promoter);
        if (userPromoteRelationList!=null && !userPromoteRelationList.isEmpty()) {
            for (UserPromoteRelation temp: userPromoteRelationList) {
                int level = temp.getRelationship();
                if (level>=PROMOTE_LEVEL) {
                    continue;
                }
                userPromoteRelation = new UserPromoteRelation();
                userPromoteRelation.setParentId(temp.getParentId());
                userPromoteRelation.setRelationship(level+1);
                userPromoteRelation.setUserId(bePromoter);
                needSaveData.add(userPromoteRelation);
            }
        }
        userPromoteRelationDao.saveBatch(needSaveData);
        return true;
    }

    /**
     * @Description  我的团队
     * @Param [jsonObject]
     * @author ZhaoPing'an
     * @Date 2018/5/21 12:28
     */
    @Override
    public ResponseVo myTeam(int pageNum, int pageSize) {
        pageNum = pageNum<1?1:pageNum;
        pageNum -=1;
        pageSize = pageSize<1?10:pageSize;
        String parentId = WebUtils.getUser().getId();

        //只查询直属推荐人
        Page<UserPromoteRelation> userPromoteRelationList = userPromoteRelationDao.findByParentIdAndRelationship(parentId,1,PageRequest.of(pageNum, pageSize));
        Integer count = userPromoteRelationDao.findByParentIdAndRelationship(WebUtils.getUser().getId());

        Map<String,Object> resData = new HashMap<>();

        if (null!=userPromoteRelationList && !userPromoteRelationList.getContent().isEmpty()) {
            List<String> userIds = new ArrayList<>();
            for (UserPromoteRelation userPromoteRelation :userPromoteRelationList) {
                userIds.add(userPromoteRelation.getUserId());
            }
            List<User> userList = userDao.findByIdIn(userIds);

            resData.put("total",userPromoteRelationList.getTotalElements());
            resData.put("rows",userList);
            resData.put("count",count);

        } else {
            resData.put("total",0);
            resData.put("rows",new ArrayList<>());
            resData.put("count",count);
        }

        return ResponseVo.success().setData(resData);
    }
}
